package com.qyl.honghengshop.controller.admin;


import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qyl.honghengshop.common.PageConstant;
import com.qyl.honghengshop.dto.Goods;
import com.qyl.honghengshop.dto.TimeLimit;
import com.qyl.honghengshop.service.GoodsService;
import com.qyl.honghengshop.service.TimeService;
import com.qyl.honghengshop.service.impl.GoodsServiceImpl;
import com.qyl.honghengshop.service.impl.TimeServiceImpl;
import com.qyl.honghengshop.utils.PageBean;

@Controller
@RequestMapping("/admin/time")
public class TimeController {

	private TimeService timeService = new TimeServiceImpl();
	
	private GoodsService goodsService = new GoodsServiceImpl();
	
	@RequestMapping("/timeList")
	public String timeList(Integer pageIndex,Integer pageSize,ModelMap map,TimeLimit time){
		
		//1.计算总页数
		int recordCount = timeService.getRecordCount(time);
		
		//2.创建pageBean
		PageBean pageBean = new PageBean(pageIndex, recordCount,pageSize);
		
		//获得有记录集合的pageBean
		timeService.timeList(pageBean,time);
		
		map.put("pageBean", pageBean);
		
		//回显查询数据
		map.put("time", time);
		
		return "time/timeList";
	}
	@RequestMapping("/toAdd")
	public String toAdd(ModelMap map){
		
		List<Goods> shopList = goodsService.getAllShop();
		
		map.put("shopList", shopList);
		
		return "time/timeAdd";
	}
	@RequestMapping("/timeAdd")
	public String timeAdd(TimeLimit time,ModelMap map){
		
		int count = timeService.timeAdd(time);
		
		if(count > 0){
			map.put(PageConstant.TIP, "限时添加成功");
		}else{
			map.put(PageConstant.TIP, "限时添加失败");			
		}
		
		return "forward:toAdd";	
	}
	@RequestMapping("/delTime")
	public String delTime(Integer id,ModelMap map){
		
		int count = timeService.delTime(id);
		
		if(count > 0){
			map.put(PageConstant.TIP, "限时删除成功");
		}else{
			map.put(PageConstant.TIP, "限时删除失败");			
		}
		return "forward:timeList";
	}
	
	@RequestMapping("/toTimeUpdate")
	public String toTimeUpdate(Integer id,ModelMap map){
		
		TimeLimit time = timeService.getTimeById(id);
		
		List<Goods> shopList = goodsService.getAllShop();
		
		map.put("shopList", shopList);
		
		map.put("time", time);
		
		return "time/timeUpdate";
	}
	@RequestMapping("/timeUpdate")
	public String timeUpdate(TimeLimit time,ModelMap map){
		
		int count = timeService.timeUpdate(time);
		
		if(count > 0){
			map.put(PageConstant.TIP, "限时修改成功");
		}else{
			map.put(PageConstant.TIP, "限时修改失败");			
		}
		
		return "forward:toTimeUpdate";			
	}
	
	
}
